home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Magazine Collection 2001
/
Delphi Magazine Collection 20001 (2001).iso
/
DISKS
/
Issue35
/
eval_dan
/
EVAL_DAN.ZIP
/
mathtest.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-12-30
|
4KB
|
154 lines
unit mathtest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
mathcomp, StdCtrls, ComCtrls, Tabnotbk;
type
TForm1 = class(TForm)
EDT_Expression: TEdit;
BTN_MakeTree: TButton;
TreeView: TTreeView;
BTN_Evaluate: TButton;
LBL_Value: TLabel;
Notebook: TTabbedNotebook;
MEMO_Tokens: TMemo;
LB_Vars: TListBox;
Label1: TLabel;
EDT_VarName: TEdit;
Label2: TLabel;
EDT_VarData: TEdit;
BTN_Set: TButton;
BTN_Delete: TButton;
BTN_Clear: TButton;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BTN_MakeTreeClick(Sender: TObject);
procedure BTN_EvaluateClick(Sender: TObject);
procedure BTN_SetClick(Sender: TObject);
procedure BTN_DeleteClick(Sender: TObject);
procedure BTN_ClearClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure UpdateVarsLB;
end;
var
Form1: TForm1;
Tree: TExpressionTree;
implementation
{$R *.DFM}
procedure TForm1.UpdateVarsLB;
var
i: longint;
PVar: PVariableRecord;
begin
if Tree.VarList.Count > 0 then
begin
LB_Vars.Clear;
for i := 0 to Tree.Varlist.count-1 do
begin
PVar := PVariableRecord(Tree.VarList.Items[i]);
LB_Vars.Items.Add(PVar^.VarName+': '+FloatToStr(PVar^.VarData));
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Tree := TExpressionTree.Create;
Tree.VarList := TVarList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Tree.VarList.Free;
Tree.Free;
end;
procedure TForm1.BTN_MakeTreeClick(Sender: TObject);
procedure DoTree(CNode: TExpressionNode; ONode: TTreeNode);
var
i: longint;
begin
ONode := TreeView.Items.AddChild(ONode,CNode.Value);
if CNode.Children.Count>0 then
begin
for i := 0 to CNode.Children.Count-1 do
DoTree(TExpressionNode(CNode.Children.items[i]),ONode);
end;
end;
begin
Tree.Expression := EDT_Expression.Text;
MEMO_Tokens.Lines.Assign(Tree.Tokens);
if Assigned(Tree.TopNode) then
begin
TreeView.Items.Clear;
DoTree(Tree.TopNode,TreeView.Selected);
TreeView.FullExpand;
end;
BTN_Evaluate.Enabled := True;
end;
procedure TForm1.BTN_EvaluateClick(Sender: TObject);
begin
if Assigned(Tree.TopNode) then
begin
Tree.TopNode.Evaluate(False,True);
LBL_Value.Caption := FloatToStr(Tree.TopNode.NumValue);
if not Tree.TopNode.ValidValue then
LBL_Value.Caption := LBL_Value.Caption + ' [Not valid]';
UpdateVarsLB;
end;
end;
procedure TForm1.BTN_SetClick(Sender: TObject);
var
tf: double;
begin
if EDT_VarName.Text<> '' then
begin
try
tf := StrToFloat(EDT_VarData.Text);
//It worked--create new variable and update
Tree.VarList.NewVar(EDT_VarName.Text,tf);
UpdateVarsLB;
except
on EConvertError do
begin
ShowMessage(EDT_VarData.Text+' is not a valid numeric value.');
Exit;
end;
end;
end;
end;
procedure TForm1.BTN_DeleteClick(Sender: TObject);
begin
if (LB_Vars.ItemIndex > -1) and (Tree.VarList.Count>0) then
begin
Dispose(PVariableRecord(Tree.VarList.items[LB_Vars.ItemIndex]));
Tree.VarList.Delete(LB_Vars.ItemIndex);
UpdateVarsLB;
end;
end;
procedure TForm1.BTN_ClearClick(Sender: TObject);
begin
Tree.VarList.Free;
Tree.VarList := TVarList.Create;
LB_Vars.Clear;
end;
end.